﻿/*=========================================================================
 * AUTH: Vincent Breton
 * DESC: Text Scroller
 * REF: Stellar Siege Project 2010
 *=========================================================================*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace RisingThreat
{
    class TextScroller
    {
        private Vector2 velocity, position, currentPosition;
        private Rectangle drawingArea;
        private SpriteFont font;
        private List<String> stringList;
        private float updateIntervalMsec;
        private float textBottom;

        public TextScroller(String scrollText, SpriteFont font, Vector2 velocity, Vector2 position, float updateIntervalMsec)
        {
            this.velocity = velocity;
            this.position = position;
            this.updateIntervalMsec = updateIntervalMsec;
            this.font = font;
            textBottom = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;

            drawingArea.X = (GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.TitleSafeArea.X > 0)
                ? GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.TitleSafeArea.X : 20;
            drawingArea.Y = (0 < GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.TitleSafeArea.Y)
                ? GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.TitleSafeArea.Y : 0;
            drawingArea.Height = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
            drawingArea.Width = (GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.TitleSafeArea.Width < GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width)
                ? (GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.TitleSafeArea.Right - drawingArea.X) : (GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width - drawingArea.X - 20);

            reflowText(scrollText);
        }

        private void reflowText(String scrollText)
        {
            String line = "";
            stringList = new List<String>();
            int startIndex = 0, endIndex = 1;
            char[] searchChars = new char[] { ' ', '\n' };

            if (scrollText == "")
                stringList.Add(" ");
            else
            {
                do
                {
                    if ((endIndex = scrollText.IndexOfAny(searchChars, startIndex)) == -1)
                        endIndex = scrollText.Length - 1;

                    if (font.MeasureString(line + scrollText.Substring(startIndex, endIndex - startIndex + 1)).X < drawingArea.Width)
                    {
                        line += scrollText.Substring(startIndex, endIndex - startIndex + 1);

                        if (scrollText[endIndex] == '\n')
                        {
                            if (line != "\n")
                                stringList.Add(line.Substring(0, line.Length - 1));
                            else
                                stringList.Add(" ");
                            line = "";
                        }

                        startIndex = (endIndex + 1) < scrollText.Length ? endIndex + 1 : scrollText.Length - 1;
                    }
                    else
                    {
                        if (line.Length > 0)
                            stringList.Add(line);
                        line = "";
                    }
                }
                while (startIndex != endIndex);

                if ((stringList.Count == 0) || (line != stringList[stringList.Count - 1]))
                    stringList.Add(line);
            }
        }

        virtual public void Update(float msecElapsed)
        {
            float intervals = msecElapsed / updateIntervalMsec;

            if (intervals > 0)
            {
                position += (float) intervals * velocity;
            }
        }

        virtual public void Draw(SpriteBatch spriteBatch)
        {
            currentPosition.Y = position.Y;

            foreach (String line in stringList)
            {
                currentPosition.X = drawingArea.X + drawingArea.Width / 2 - font.MeasureString(line).X / 2;
                spriteBatch.DrawString(font, line, currentPosition, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
                currentPosition.Y += font.MeasureString(line).Y;
            }

            textBottom = currentPosition.Y;
        }

        public float TextBottom
        {
            get
            {
                return textBottom;
            }
        }
    }
}
